
/**
 * tweet.cpp
 * 
 * Tweet holds a single tweet entry
 * 
 * Copyright (c) 2008 Harunobu Oyama <nobukichi at gmail.com>
 * Licensed under GNU GPL version 3
 *
 */

#include	<QStringList>
#include	"tweet.h"

Tweet::Tweet(const QDateTime& timeStmp, const QString& name, const QString& txt, const QString& imageUrl)
  : createdAt(timeStmp), screenName(name), status(txt), profileImageUrl(imageUrl)
{
}

Tweet::Tweet(const Tweet& rhs)
  : createdAt(rhs.createdAt), screenName(rhs.screenName), status(rhs.status), profileImageUrl(rhs.profileImageUrl)
{
}

Tweet& Tweet::operator=(const Tweet& rhs)
{
	if (this != &rhs) {
		createdAt = rhs.createdAt;
		screenName = rhs.screenName;
		status = rhs.status;
		profileImageUrl = rhs.profileImageUrl;
	}
	return *this;
}
	
void Tweet::setStatusText(const QString& txt)
{
	status = txt;
}

//! returns empty string ("") if the statusText does not begin with @userName
QString Tweet::atUser() const
{
	if (status.startsWith("@")) {
		int tailPos = status.indexOf(" ");
		if (0 < tailPos) {
			return status.mid(1, tailPos-1);
		}
	}
	return "";
}

//! returns statusText after removing @user at the top
QString Tweet::tweetText() const
{
	if (status.startsWith("@")) {
		int tailPos = status.indexOf(" ");
		if (0 < tailPos) {
			return status.mid(tailPos);
		}
	}
	return status;
}

//! ==== FORMAT ====
//! "yyyy/M/d hh:mm:ss\tscreenName\tprofileImageUrl\tsingleLineText"
//!  [0]--------------  [1]-------  [2]------------  [3]-----------   
//! where "\\" and "\n" in status are encoded to "\\\\" and "\\n" in singleLineText  

const char* const DATE_TIME_FORMAT = "yyyy/M/d hh:mm:ss";

QString Tweet::toString() const
{
	QString format("%1\t%2\t%3\t%4");
	QString singlieLineText = status;
	singlieLineText.replace("\\", "\\\\").replace("\n", "\\n");
	return format.arg(createdAt.toString(DATE_TIME_FORMAT)).arg(screenName).arg(profileImageUrl).arg(singlieLineText);
}

Tweet Tweet::fromString(const QString& str)
{
	QStringList stringList = str.split("\t");
	if (stringList.size() != 4) {
		return Tweet();
	}
	
	QDateTime dateTime = QDateTime::fromString(stringList[0], DATE_TIME_FORMAT);
	QString originalText = stringList[3];
	originalText.replace("\\n", "\n").replace("\\\\", "\\");
	return Tweet(dateTime, stringList[1], originalText, stringList[2]);
}

//! operators so that we can store Tweet in std::set
bool operator==(const Tweet& lhs, const Tweet& rhs)
{
	return (lhs.timeStamp() == rhs.timeStamp() && lhs.userName() == rhs.userName() && lhs.statusText() == rhs.statusText());
}

bool operator<(const Tweet& lhs, const Tweet& rhs)
{
	//! put latest updates on the top of the list 
	if (lhs.timeStamp() > rhs.timeStamp()) {
		return true;
	} else if (rhs.timeStamp() > lhs.timeStamp()) {
		return false;
	} else {
		//! and then compare by user name
		if (lhs.userName() < rhs.userName()) {
			return true;
		} else if (rhs.userName() < lhs.userName()) {{}
			return false;
		} else {
			//! compare the status, if timestamp and user name are both equal
			return (lhs.statusText() < rhs.statusText());
		}
	}
}

